package edu.tyut.game.controller.user;

import com.alibaba.fastjson2.JSON;
import edu.tyut.game.model.Result;
import edu.tyut.game.model.User;
import edu.tyut.game.service.FlyingFlowerOrderService;
import edu.tyut.game.service.UserService;
import edu.tyut.game.utils.ImageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/hello")
    public String hello(){
        return "Hello 世界!";
    }

    private UserService userService;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    private FlyingFlowerOrderService orderService;
    @Autowired
    public void setOrderService(FlyingFlowerOrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 用户注册
     * @param userJson 注册用户实体JSON字符串
     * @return 返回请求参数
     */
    @PostMapping("/register")
    public Result<String> register(@RequestBody String userJson){
        try {
            User user = JSON.parseObject(userJson, User.class);
            userService.register(user);
            User userByUsername = userService.getUserByUsername(user.username());
            log.info("添加游戏数据");
            orderService.firstAddData(userByUsername.id());
            return Result.success("注册成功", null);
        }catch (Exception e){
            log.error("注册失败, 错误原因：" + e.getMessage());
            return Result.error("注册失败", null);
        }
    }

    /**
     * 修改密码
     * @param map 包含用户名，新密码
     * @return 传回结果
     */
    @PostMapping("/updatePassword")
    public Result<Boolean> updatePassword(@RequestBody Map<String, String> map){
        String username = map.get("username");
        String password = map.get("password");
        userService.updatePassword(username, password);
        return Result.success("修改密码成功", true);
    }

    /**
     * 登录逻辑
     * @param map 包含用户名，输入的密码
     * @return 传回结果
     */
    @PostMapping("/login")
    public Result<User> login(@RequestBody Map<String, String> map){
        String username = map.get("username");
        String password = map.get("password");
        User user = userService.getUserByUsername(username);
        if (user.password().equals(password)){
            return Result.success("登陆成功", user);
        }
        return Result.error("登陆失败", null);
    }

    @GetMapping("/getUser/{username}")
    public Result<User> getUser(@PathVariable("username") String username){
        User user = userService.getUserByUsername(username);
        log.debug("getUser: " + user);
        if (user != null){
            return Result.success("获取用户成功!", user);
        }
        return Result.success("未获取用户名!", null);
    }

    @PostMapping("/existUser")
    public Result<Boolean> existUser(@RequestBody Map<String, String> map){
        log.debug(map.toString());
        String username = map.get("username");
        User user = userService.getUserByUsername(username);
        if (user != null){
            return Result.success("已存在用户名", true);
        }
        return Result.success("不存在用户名", false);
    }

    // 上传头像
    @PostMapping("/updateHeadIcon")
    public  Result<Boolean> updateHeadIcon(@RequestParam("file") MultipartFile file, @RequestParam("uid") int uid){
        String path = ImageUtil.saveImage(file);
        userService.updateHeadIcon(uid, path);
        return Result.success(path, true);
    }
}
